package com.example.service;

import com.example.controller.DoctorController;
import com.example.mapper.DoctorMapper;
import com.example.mapper.UserMapper;
import com.example.pojo.Doctor;
import com.example.pojo.DoctorQuery;
import com.example.pojo.Result;
import com.example.pojo.User;
import com.example.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class DoctorService {

    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private UserService userService;
    @Autowired
    private  UserMapper userMapper;

    @Autowired
    private User user;

    /**
     * 查询
     * @param doctorQuery
     * @return
     */
    public List<Doctor> getUserList(DoctorQuery doctorQuery) {

        return doctorMapper.getDoctorList(doctorQuery);

    }

    /**
     * 增加医师信息
     * @param doctor
     * @return
     */
    public Result addDoctorInformation(Doctor doctor) {
        // 校验用户名是否为空
        if (!StringUtils.hasText(doctor.getName())) {
            return new Result(100, "姓名不能为空", null);
        }
        // 校验年龄是否为空
        if (doctor.getAge() == null) {
            return new Result(100, "年龄不能为空", null);
        }
        // 校验性别是否为空
        if (doctor.getSex() == null) {
            return new Result(100, "性别不能为空", null);
        }
        // 校验电话是否为空
        if (!StringUtils.hasText(doctor.getPhone())) {
            return new Result(100, "电话不能为空", null);
        }
        // 校验类型ID是否为空
        if (doctor.getType_id() == null) {
            return new Result(100, "诊治类型不能为空", null);
        }
        // 校验医院是否为空
        if (!StringUtils.hasText(doctor.getHospital())) {
            return new Result(100, "医院不能为空", null);
        }
        // 根据级别ID设置医生级别
        if (doctor.getLevel_id() != null) {
            switch (doctor.getLevel_id()) {
                case 1:
                    doctor.setDoctorLevel("主任医师");
                    break;
                case 2:
                    doctor.setDoctorLevel("普通医师");
                    break;
                case 3:
                    doctor.setDoctorLevel("实习医师");
                    break;
                default:
                    return new Result(100, "无效的级别ID", null);
            }
        } else {
            return new Result(100, "级别ID不能为空", null);
        }


        // 创建新账号

        int count = userMapper.countSameRealname(doctor.getName());

        //初始密码为电话号码
        //String md5Pwd= MD5Utils.stringToMD5(doctor.getPhone());
        //初始用户名，确保唯一性
        String uname="";
        if(count==0){
            uname=doctor.getName();
        }else{
           uname=doctor.getName()+count;
        }
        user.setUname(uname);
        user.setPwd(doctor.getPhone());
        user.setRealname(doctor.getName());
        //医师类型为 ROlE_2
        user.setUtype("ROLE_2");
        user.setPhonenumber(doctor.getPhone());

        //先注册医师信息
        doctor.setCreatetime(new Date());
        doctor.setUpdatetime(new Date());

        //再注册账号
        Result res=userService.addUserInformation(user);
        if(res.getCode()!=200){
            return new Result(100,"操作失败",null);
        }

        Map<String, String> map = new HashMap<>();

        map.put("password",doctor.getPhone());
        map.put("uname",user.getUname());

        return new Result(200, "医生信息添加成功", map);
    }


    public Result insertByAccount(Doctor doctor) {

        // 校验用户名是否为空
        if (!StringUtils.hasText(doctor.getName())) {
            return new Result(100, "用户名不能为空", null);
        }
        // 校验年龄是否为空
        if (doctor.getAge() == null) {
            return new Result(100, "年龄不能为空", null);
        }
        // 校验性别是否为空
        if (doctor.getSex() == null) {
            return new Result(100, "性别不能为空", null);
        }
        // 校验电话是否为空
        if (!StringUtils.hasText(doctor.getPhone())) {
            return new Result(100, "电话不能为空", null);
        }
        // 校验类型ID是否为空
        if (doctor.getType_id() == null) {
            return new Result(100, "类型ID不能为空", null);
        }
        // 校验医院是否为空
        if (!StringUtils.hasText(doctor.getHospital())) {
            return new Result(100, "医院不能为空", null);
        }
        //校验诊治类型
        if (doctor.getTreatType()==null) {
            return new Result(100, "诊治类型不能为空", null);
        }

        // 根据级别ID设置医生级别
        if (doctor.getLevel_id() != null) {
            switch (doctor.getLevel_id()) {
                case 1:
                    doctor.setDoctorLevel("主任医师");
                    break;
                case 2:
                    doctor.setDoctorLevel("普通医师");
                    break;
                case 3:
                    doctor.setDoctorLevel("实习医师");
                    break;
                default:
                    return new Result(100, "无效的级别ID", null);
            }
        } else {
            return new Result(100, "级别ID不能为空", null);
        }
        if (!StringUtils.hasText(doctor.getPwd())) {
            return new Result(100, "密码不能为空", null);
        }

        /**
         * 先修改account信息
         */
        //获取Account
        User user=userMapper.getUserByAccount(doctor.getAccount_id());

        String md5pwd=MD5Utils.stringToMD5(doctor.getPwd());

        user.setPwd(md5pwd);

        user.setUpdatetime(new Date());

        int row=userMapper.update(user);
        if(row!=1){
            return new Result(100,"操作失败",null);
        }

        /**
         * 处理doctor
         */
        int row1=doctorMapper.update(doctor);
        if(row1!=1){
            return new Result(100,"操作失败",null);
        }

        return new Result(200, "修改成功", null);
    }

    public Result delDoctorById(int id) {
        /**
         * 查询是否存在
         */
        Doctor d=doctorMapper.getDoctorById(id);
        if(d==null){
            return new Result(100,"医师不存在",null);
        }
        /**
         * 先删除用户表账号
         */
        int row =doctorMapper.delDoctorById(id);

        if(row!=1){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"删除成功",null);

    }

    public Result delBatch(String ids) {

        if (!StringUtils.hasText(ids)){
            return new Result(100,"请至少选择一条记录",null);
        }

        String[] idStr =ids.split(",");

        List<Integer> idList=new ArrayList<>();

        for (int i=0; i<idStr.length;i++){
            idList.add(Integer.parseInt(idStr[i]));
        }
        int rows =doctorMapper.delBatch(idList);
        if(rows!=idList.size()){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"删除成功",null);
    }


}
